/** 
 * @file Iconv.c
 * @Synopsis  iconv
 * @author JzeTech 
 * @version 0.1 beta
 * @date 2009-08-27
 */
#include <string.h>
#include "Iconv.h"
/* --------------------------------------------------------------------------*/
/** 
 * @Synopsis  
 * 
 * @Param sourcebuf
 * @Param sourcelen
 * @Param destbuf
 * @Param destlen
 * 
 * @Returns   
 */
/* ----------------------------------------------------------------------------*/
int Utf8ToGb2312(char *sourcebuf,size_t sourcelen,char *destbuf,size_t destlen)
{
	  iconv_t cd;
	  if( (cd = iconv_open("gb2312","utf-8")) ==0 )
	      return -1;
	  memset((void*)destbuf,0,destlen);
	  char **source = &sourcebuf;
	  char **dest = &destbuf;
	  if(-1 == iconv(cd,source,&sourcelen,dest,&destlen))
	      return -1;
	  iconv_close(cd);
	  return 0;
					  
}


/* --------------------------------------------------------------------------*/
/** 
 * @Synopsis  
 * 
 * @Param sourcebuf
 * @Param sourcelen
 * @Param destbuf
 * @Param destlen
 * 
 * @Returns   
 */
/* ----------------------------------------------------------------------------*/
int Gb2312ToUtf8(char *sourcebuf,size_t sourcelen,char *destbuf,size_t destlen)
{
	  iconv_t cd;
	  if( (cd = iconv_open("utf-8","gb2312")) ==0 )
	      return -1;
	  memset(destbuf,0,destlen);
	  char **source = &sourcebuf;
	  char **dest = &destbuf;
	  if(-1 == iconv(cd,source,&sourcelen,dest,&destlen))
	      return -1;
	  iconv_close(cd);
	  return 0;
}
int Utf8ToUnicode(char *sourcebuf,size_t sourcelen,char *destbuf,size_t destlen)
{
	  iconv_t cd;
	  if( (cd = iconv_open("UTF-16LE","utf-8")) ==0 )
	      return -1;
	  memset((void*)destbuf,0,destlen);
	  char **source = &sourcebuf;
	  char **dest = &destbuf;
	  if(-1 == iconv(cd,source,&sourcelen,dest,&destlen))
	      return -1;
	  iconv_close(cd);
	  return 0;
}
